package bd

import WaterMarkAdapter
import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.WindowManager
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import oa.lla.cca.databinding.ActivityImgDetailsBinding
import java.io.File
import java.text.SimpleDateFormat
import java.util.*


class CH : FG() {
    private var currentPosition = 0
    private var disposeDialog: FC? = null
    private var dataBing: ActivityImgDetailsBinding? = null
    private val mformatType: String = "yyyy/MM/dd HH:mm:ss";
    private var currentImg: FB? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        dataBing = ActivityImgDetailsBinding.inflate(layoutInflater)
        setContentView(dataBing?.root)
        initView()
    }


    companion object {
        val POSITION = "position"
        val PAGE_TYPE = "PageType"
        val PAGE_IMAGE = "PageImage"
        val DETAILS_BENA = "deatils_bean"
        val DETAILS_CODE = 102
        fun intentImgDetailsActivity(context: Activity, bean: FB, position: Int) {
            val intent = Intent(context, CH::class.java)
            intent.putExtra(POSITION, position)
            intent.putExtra(DETAILS_BENA, bean)
            context.startActivityForResult(intent, DETAILS_CODE)
        }
    }


    private fun initView() {
        dataBing?.apply {
            backLeft.setOnClickListener { finish() }
            val waterGrid = object :GridLayoutManager(this@CH,3){
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            watermarkList.layoutManager = waterGrid
            watermarkList.adapter = WaterMarkAdapter(this@CH)

            currentPosition = intent.getIntExtra(POSITION, 0)
            currentImg = intent.getSerializableExtra(DETAILS_BENA) as FB
            if (currentImg != null) {
                setImageInfo(currentImg!!)
                Glide.with(this@CH).asBitmap().load(currentImg!!.path).into(detailsImg)
            }

            //立即删除
            bottomDelete.setOnClickListener {
                if (currentImg != null) {
                    showAd(true)
                }
            }

            //保存到相册
            bottomRecover.setOnClickListener {
                showAd(false)
            }

        }

    }

    private fun saveLocation(str: String) {
        //通过Glide 生成Bitmap
        Glide.with(this).asBitmap().load(str).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                val saveBitmap2Gallery =
                    FE.saveBitmapGallery(this@CH, resource)
                if (saveBitmap2Gallery) {
                    Toast.makeText(this@CH, "保存成功\n请前去相册查看", Toast.LENGTH_SHORT)
                        .show()
                } else {
                    Toast.makeText(this@CH, "保存失败", Toast.LENGTH_SHORT).show()
                }

            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                super.onLoadFailed(errorDrawable)
            }
        })
    }



    private fun toFinish(){
        val intent = Intent()
        intent.putExtra(POSITION,currentPosition)
        intent.putExtra(PAGE_TYPE, PAGE_IMAGE)
        setResult(Activity.RESULT_OK,intent)
        finish()
    }



    private fun showAd(isDelete: Boolean){
        if (isDelete){
            File(currentImg?.path!!).delete()
            toFinish()
        }else{
            if (currentImg != null) {
                saveLocation(currentImg?.path!!)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        disposeDialog?.cancel()
    }


    //展示文件创建时间和文件路径
    private fun setImageInfo(currentImg: FB) {
        if (currentImg == null) {
            return
        }
        val cal: Calendar = Calendar.getInstance();
        val formatter = SimpleDateFormat(mformatType);
        cal.timeInMillis = currentImg.createTime;
        val time = formatter.format(cal.time);
        dataBing?.fileInfo!!.text = "创建时间：${time}\n文件路径：${currentImg.path}"
    }

}